package com.mioto.ld.communication.mqtt.protocol;

import cn.hutool.core.collection.CollUtil;
import cn.hutool.core.date.DateUtil;
import cn.hutool.core.util.ObjectUtil;
import com.mioto.ld.communication.mqtt.protocol.cache.DeviceInfoCache;
import com.mioto.ld.communication.mqtt.protocol.cache.ReportAttributeCache;
import com.mioto.ld.communication.mqtt.protocol.model.AlertLogCondition;
import com.mioto.ld.communication.mqtt.protocol.model.Message;
import com.mioto.ld.communication.mqtt.protocol.model.device.DeviceFaultReportPayload;
import com.mioto.ld.module.alert.model.AlertTrigger;
import com.mioto.ld.module.alert.model.TriggerTargetType;
import com.mioto.ld.module.alert.service.IAlertTriggerService;
import com.mioto.ld.module.device.model.Device;
import com.mioto.ld.module.device.service.IDeviceAlertLogService;
import com.mioto.ld.module.device.service.IDeviceFaultLogService;
import com.mioto.ld.module.device.service.IDeviceService;
import com.mioto.ld.module.product.service.IProductAttributeService;
import com.mioto.ld.module.product.service.IProductEventService;
import com.mioto.ld.utils.SpringBeanUtil;
import lombok.extern.slf4j.Slf4j;

import java.util.*;
import java.util.concurrent.Executor;

/**
 * @author mioto-qinxj
 * @date 2024/5/31
 * @desc 设备故障恢复上报
 */
@Slf4j
public class FaultRecoveryHandleStrategy implements IReceiptHandleStrategy {

    private FaultReportTriggerHandler faultReportTriggerHandler;

    public FaultRecoveryHandleStrategy(){
        faultReportTriggerHandler = new FaultReportTriggerHandler();
    }

    @Override
    public void protocolHandler(Message message) throws Exception {
        log.info("---------------------设备 - {} 故障恢复上报消息处理开始---------------------",message.getSn());
        log.info("update device fault log");
        faultReportTriggerHandler.handle(message);
        log.info("---------------------设备 - {} 故障恢复上报消息处理结束---------------------",message.getSn());
    }

    /**
     * 告警配置故障恢复上报处理
     * @param device
     * @param payload
     *//*
    private void alertRecoveryProcess(Device device,  DeviceFaultReportPayload payload){
        log.info("check device alert triggers,if meet alert recovery condition,update alert log to recovery status");
        List<AlertTrigger>  alertTriggers = SpringBeanUtil.getBean(IAlertTriggerService.class)
                .findEventTriggersByDeviceId(device.getId());
        int alertId = 0;
        boolean compareResult = false;
        Map<Integer,String> sysEventMap = SpringBeanUtil.getBean(IProductEventService.class)
                .findEventByDeviceIdAndIdentitys(device.getId(),payload.getFaults());
        if (CollUtil.isNotEmpty(sysEventMap)) {
            Map<String,Object> reportAttr = ReportAttributeCache.getAttributes(device.getSn());
            Map<Integer,String> map;
            Set<Integer> alertIds = new HashSet<>(2);
            for (AlertTrigger alertTrigger : alertTriggers) {
                //同一个告警配置，如果有一个条件失败，直接判断下一个告警配置
                if (alertId == alertTrigger.getAlertId() && !compareResult){
                    continue;
                }else if (alertId != alertTrigger.getAlertId() && compareResult){
                    alertIds.add(alertId);
                }
                if (ObjectUtil.equal(alertTrigger.getTriggerTarget(), TriggerTargetType.TRIGGER_TARGET_TYPE_EVENT)) {
                    compareResult = sysEventMap.containsKey(alertTrigger.getTargetId()) ? true : false;
                } else if (ObjectUtil.equal(alertTrigger.getTriggerTarget(), TriggerTargetType.TRIGGER_TARGET_TYPE_DEVICE)){
                    if (CollUtil.isNotEmpty(reportAttr)){
                        map = SpringBeanUtil.getBean(IProductAttributeService.class)
                                .findIdAndIdentityByProductIdAndAttr(device.getProductId()
                                        ,reportAttr.keySet().toArray(new String[reportAttr.size()]));
                        compareResult =!SpringBeanUtil.getBean(TriggerProcess.class).
                                checkAttributeTriggerConditions(alertTrigger.getTriggerCondition()
                                ,alertTrigger.getTargetId(),map,reportAttr);
                    }else {
                        compareResult = false;
                    }
                }
                alertId = alertTrigger.getAlertId();
            }
            if (!alertIds.isEmpty()){
                AlertLogCondition alertLogCondition = AlertLogCondition.builder()
                        .reportAttribute(reportAttr)
                        .reportFault(sysEventMap).build();
                SpringBeanUtil.getBean(IDeviceAlertLogService.class)
                        .updateRecoveryData(device.getId(),alertIds
                                ,DateUtil.date(payload.getReportDate())
                                ,alertLogCondition.toJsonStr());
            }
        }
    }*/
}
